package me.huqiao.loganlyzer.util;

import me.huqiao.loganlyzer.main.AttPreProcessor;

import java.util.Date;

public class ProcessUtil {
    /**
     * 日期替换
     * @param str
     * @return
     */
    public static  String replaceMonth(String str){
        if(str==null)return str;
        str = str.replaceAll("Jul", "01");
        str = str.replaceAll("Feb", "02");
        str = str.replaceAll("Mar", "03");
        str = str.replaceAll("Apr", "04");
        str = str.replaceAll("May", "05");
        str = str.replaceAll("Jun", "06");
        str = str.replaceAll("Jul", "07");
        str = str.replaceAll("Aug", "08");
        str = str.replaceAll("Sep", "09");
        str = str.replaceAll("Otc", "10");
        str = str.replaceAll("Nov", "11");
        str = str.replaceAll("Dec", "12");
        return str;
    }

    public static  AttPreProcessor processor = new AttPreProcessor() {
        @Override
        public String process(String att) {
            att = att.split(" ")[0].substring(1);
            //att = att.replaceAll("Mar", "03");
            att = ProcessUtil.replaceMonth(att);
            Date date = DateUtil.parse(att);
            if(date==null){
                return "";
            }
            return DateUtil.format(date, "yyyy-MM-dd HH:mm");
        }
    };

    /**
     * 时间的函数 - 默认值
     */
    public static  AttPreProcessor processor_time = new AttPreProcessor() {
        @Override
        public String process(String attr) {
            if(StringUtil.isEmpty(attr)||"-".equals(attr)){
                return "2000";
            }else{
                return attr;
            }
        }
    };

    /**
     * 请求URL过滤时间戳参数类的
     */
    public static  AttPreProcessor processor_url = new AttPreProcessor() {
        @Override
        public String process(String attr) {
            if(!StringUtil.isEmpty(attr)){
                return attr.split("\\?")[0];
            }
            return attr;
        }
    };
}
